﻿using Abp.Dependency;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
using GN.Pay.Application.GatewayApp;
using GN.Pay.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace GN.Pay.Application.GatewayApp
{
    /// <summary>
    /// 网关处理器安装
    /// </summary>
    public class GatewayServiceInstaller
    {
        private static GatewayServiceInstaller instance = new GatewayServiceInstaller();

        /// <summary>
        /// 获取实例
        /// </summary>
        public static GatewayServiceInstaller Instance
        {
            get
            {
                return GatewayServiceInstaller.instance;
            }
        }


        private readonly Dictionary<string, GatewayServiceInfo> gatewayServices;

        private GatewayServiceInstaller()
        {
            this.gatewayServices = new Dictionary<string, GatewayServiceInfo>(StringComparer.InvariantCultureIgnoreCase);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="iocManager"></param>
        public void Register(IIocManager iocManager)
        {
            lock (this)
            {
                if (iocManager == null)
                {
                    throw new ArgumentNullException(nameof(iocManager));
                }
                var assembly = Assembly.GetExecutingAssembly();               
                var typeList = TypeUtils.GetAssignableTypes<IGatewayAppService>(assembly, s => s.IsInterface && !s.Equals(typeof(IGatewayAppService)), false);
                foreach (var type in typeList)
                {
                    var methodInfos = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);
                    foreach (var m in methodInfos)
                    {
                        if (m.IsDefined(typeof(GatewayServiceAttribute)))
                        {
                            var serviceInfo = new GatewayServiceInfo(iocManager, m);
                            gatewayServices[serviceInfo.ServiceName] = serviceInfo;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 获取所有网关处理服务
        /// </summary>
        /// <returns></returns>
        public List<GatewayServiceInfo> GetGatewayServices()
        {
            return gatewayServices.Values.ToList();
        }

        /// <summary>
        /// 获取是否注册服务
        /// </summary>
        /// <param name="serviceName">服务名称</param>
        /// <returns></returns>
        public bool IsRegisterService(string serviceName)
        {
            if (serviceName == null)
            {
                throw new ArgumentNullException(nameof(serviceName));
            }
            return this.gatewayServices.ContainsKey(serviceName);
        }

        /// <summary>
        /// 获取网关服务
        /// </summary>
        /// <param name="serviceName">通道</param>
        /// <returns></returns>
        public GatewayServiceInfo GetGatewayService(string serviceName)
        {
            if (serviceName == null)
            {
                throw new ArgumentNullException(nameof(serviceName));
            }
            GatewayServiceInfo service;
            if (gatewayServices.TryGetValue(serviceName, out service))
            {
                return service;
            }
            return null;
        }

    }
}
